//
//  BatteryTableViewCell.m
//  FLORIDA
//
//  Created by 魏延(颜魏) on 2023/8/2.
//

#import "BatteryTableViewCell.h"
#import "BlutoothGroupModel.h"
#import "BTManager.h"

@interface BatteryTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *cusNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *singleLabel;
@property (weak, nonatomic) IBOutlet UIButton *socLabel;
@property (weak, nonatomic) IBOutlet UILabel *voltageLabel;

@end

@implementation BatteryTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.voltageLabel.layer.cornerRadius = 2;
    self.voltageLabel.layer.masksToBounds = YES;
}

- (void)setModel:(BlutoothModel *)model {
    _model = model;
    self.cusNameLabel.text = model.cusName;
    self.nameLabel.text = model.name;
    NSString *single = [[[BTManager sharedBTManager].RSSIDict objectForKey:model.name] description];
    [self.singleLabel setTitle:[NSString stringWithFormat:@"%@db", single ?: @"0"] forState:UIControlStateNormal];
    NSString *battery = @"";
    NSString *voltage = @"0V";
    for (NSInteger i=0; i<[BTManager sharedBTManager].batteryLevelSource.count; i++) {
        NSDictionary *dict = [BTManager sharedBTManager].batteryLevelSource[i];
        if ([[dict objectForKey:@"name"] isEqualToString:model.name]) {
            if ([[dict objectForKey:@"batteryLevel"] integerValue] != -1) {
                battery = [NSString stringWithFormat:@" %@%%", dict[@"batteryLevel"] ?: @""];
            }
            if ([[dict objectForKey:@"voltage"] integerValue] != -1) {
                voltage = [NSString stringWithFormat:@"%@V", dict[@"voltage"] ?: @""];
            }
        }
    }
    self.voltageLabel.text = voltage;
    [self.socLabel setTitle:battery forState:UIControlStateNormal];
}

- (IBAction)actionMore:(id)sender {
    if (self.clickMore) {
        self.clickMore();
    }
}

- (IBAction)actionEdit:(id)sender {
    if (self.clickEdit) {
        self.clickEdit();
    }
}


@end
